package com.wrench.elevator.install.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import wrench.elevator.install.util.MyComparator;
import wrench.elevator.install.util.WrenchElevatorInstallException;
import wrench.elevator.install.util.WrenchElevatorInstallUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wrench.elevator.install.constant.Result;
import com.wrench.elevator.install.dao.ElevatorDao;



@Component
public class ElevatorService
{
    
    @Resource
    private ElevatorDao dao;
    
    private static final Logger logger = LoggerFactory.getLogger(ElevatorService.class);
    
    
    /*** 
     * 根据电梯ID获取设备号
     * */
    public String getDeviceAlias(String elevator_id) throws WrenchElevatorInstallException{
    	if(WrenchElevatorInstallUtil.hasNull(elevator_id)){
    		logger.error("==> param is not found , elevator_id = {} ",elevator_id);
    		throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
    	}
    	String device_alais=null;
    	try {
			device_alais=dao.getDeviceAlias(elevator_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			 throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
		}
    	
    	return device_alais;
    	
    }
    
    
    
    /**
     * 激活电梯
     */
    public void activationElevator(String province_id,String province_name,String city_id,String city_name,String district_id,String district_name,String building_id,String building_name,
        String zhuang_id,String zhuang_name,String danyuan_id,String danyuan_name,String device_alias,String start_loucen,
        String end_loucen,String user_id,String power,ModelMap params)
        throws WrenchElevatorInstallException
    {
        if (WrenchElevatorInstallUtil.hasNull(province_id,province_name, city_id,city_name,district_id,district_name,building_id,building_name,zhuang_id,zhuang_name,danyuan_id,danyuan_name,device_alias,start_loucen,end_loucen,user_id,power))
        {
            logger.error("==> param is not found , province_id = {} , province_name = {} ,  city_id = {} city_name = {} , district_id = {} , district_name = {} , building_id = {} , building_name = {} , zhuang_id = {} , zhuang_name = {} , danyuan_id = {} , danyuan_name = {} , device_alias = {} , start_loucen = {} , end_loucen = {} , power = {} , params = {}",
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                building_id,
                building_name,
                zhuang_id,
                zhuang_name,
                danyuan_id,
                danyuan_name,
                device_alias,
                start_loucen,
                end_loucen,
                user_id,
                power,
                params
                );
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.areInt(province_id,city_id,district_id,start_loucen,end_loucen,user_id))
        {
            logger.error("==> string is not int type , province_id = {} ,city_id = {} ,district_id = {} ,building_id = {} ,zhuang_id = {} ,danyuan_id = {} ,start_loucen = {} ,end_loucen = {} ,user_id = {}  ", province_id,city_id,district_id,building_id,zhuang_id,danyuan_id,start_loucen,end_loucen,user_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        params.put("province_id",province_id);
        params.put("province_name",province_name);
        params.put("city_id",city_id);
        params.put("city_name",city_name);
        params.put("district_id",district_id);
        params.put("district_name",district_name);
        params.put("building_id",building_id);
        params.put("building_name",building_name);
        params.put("zhuang_id",zhuang_id);
        params.put("zhuang_name",zhuang_name);
        params.put("danyuan_id",danyuan_id);
        params.put("danyuan_name",danyuan_name);
        params.put("device_alias",device_alias);
        params.put("start_loucen",start_loucen);
        params.put("end_loucen",end_loucen);
        params.put("user_id",user_id);
        params.put("power",power);
        int count = 0;
        try
        {
           //elevator_id = dao.activationElevator(province_id, province_name, city_id, city_name, district_id, district_name, building_id, building_name, zhuang_id, zhuang_name, danyuan_id, danyuan_name, device_alias, start_loucen, end_loucen, user_id, power);
            count = dao.activationElevator(params);
        }
        catch (Exception e)
        {
            logger.error("==> dao.activationElevator(province_id, province_name, city_id, city_name, district_id, district_name, building_id, building_name, zhuang_id, zhuang_name, danyuan_id, danyuan_name, device_alias, start_loucen, end_loucen, user_id, power) execute error , province_id = {} , province_name = {} ,  city_id = {} city_name = {} , district_id = {} , district_name = {} , building_id = {} , building_name = {} , zhuang_id = {} , zhuang_name = {} , danyuan_id = {} , danyuan_name = {} , device_alias = {} , start_loucen = {} , end_loucen = {} , power = {} ",
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                building_id,
                building_name,
                zhuang_id,
                zhuang_name,
                danyuan_id,
                danyuan_name,
                device_alias,
                start_loucen,
                end_loucen,
                user_id,
                power,
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.activationElevator(province_id, province_name, city_id, city_name, district_id, district_name, building_id, building_name, zhuang_id, zhuang_name, danyuan_id, danyuan_name, device_alias, start_loucen, end_loucen, user_id, power) execute success , province_id = {} , province_name = {} ,  city_id = {} city_name = {} , district_id = {} , district_name = {} , building_id = {} , building_name = {} , zhuang_id = {} , zhuang_name = {} , danyuan_id = {} , danyuan_name = {} , device_alias = {} , start_loucen = {} , end_loucen = {} , power = {} ", 
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            building_id,
            building_name,
            zhuang_id,
            zhuang_name,
            danyuan_id,
            danyuan_name,
            device_alias,
            start_loucen,
            end_loucen,
            user_id,
            power);
        
        if (count == 0){
            logger.debug("==> elevator is be active ");
            throw new WrenchElevatorInstallException(Result.ELEVATOR_IS_BE_ACTIVE_CODE, Result.ELEVATOR_IS_BE_ACTIVE_MESSAGE);
       
        }
        Object elevator_id = params.get("elevator_id");
        if (elevator_id == null)
        {
            logger.debug("==> activetion elevator fail ");
            throw new WrenchElevatorInstallException(Result.ACTIVETION_ELEVATOR_FAIL_CODE, Result.ACTIVETION_ELEVATOR_FAIL_MESSAGE);
        }
        params.remove("province_id");
        params.remove("province_name");
        params.remove("city_id");
        params.remove("city_name");
        params.remove("district_id");
        params.remove("district_name");
        params.remove("building_id");
        params.remove("building_name");
        params.remove("zhuang_id");
        params.remove("zhuang_name");
        params.remove("danyuan_id");
        params.remove("danyuan_name");
        params.remove("device_alias");
        params.remove("start_loucen");
        params.remove("end_loucen");
        params.remove("user_id");
        params.remove("power");
        //elevator_id转int报类型转化错误，java.lang.Long cannot be cast to java.lang.String
       // return elevator_id;
    }
    
    /**
     * 设置电梯楼层名称和性质
     */
    public void setElevatorInfo(String elevator_id,String loucen_info)
     throws WrenchElevatorInstallException
     {
        if (WrenchElevatorInstallUtil.hasNull(elevator_id,loucen_info))
        {
            logger.error("==> param is not found , elevator_id = {} ,loucen_info = {} ",
                elevator_id,
                loucen_info);
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.areInt(elevator_id))
        {
            logger.error("==> string is not int type , elevator_id = {}  ", elevator_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        int commit = -1;
        try
        {
           commit = dao.setLoucenInfo(elevator_id, loucen_info);
        }
        catch (Exception e)
        {
            logger.error("==> dao.setLoucenInfo(elevator_id, loucen_info) execute error , elevator_id = {} ,loucen_info = {} , error = {}  ",
                elevator_id,
                loucen_info,
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.setLoucenInfo(elevator_id,loucen_info) execute success , elevator_id = {} ,loucen_info = {} ", 
            elevator_id,
            loucen_info);
        if (commit < 0)
        {
            logger.debug("==> set elevator loucen info fail ");
            throw new WrenchElevatorInstallException(Result.SET_ELEVATOR_INFO_FAIL_CODE, Result.SET_ELEVATOR_INFO_FAIL_MESSAGE);
        
        }
     }
    
    /**
     * 查看电梯按键详情
     */
    public String locaElevatorLoucenInfo(String elevator_id)
     throws WrenchElevatorInstallException
     {
        if (WrenchElevatorInstallUtil.hasNull(elevator_id))
        {
            logger.error("==> param is not found , elevator_id = {}  ",
                elevator_id
                );
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchElevatorInstallUtil.areInt(elevator_id))
        {
            logger.error("==> string is not int type , elevator_id = {}  ", elevator_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        String elevator_loucen_info = null;
        try
        {
            elevator_loucen_info = dao.loadElevatorInfo(elevator_id);
        }
        catch (Exception e)
        {
            logger.error("==> dao.loadElevatorInfo(elevator_id) execute error , elevator_id = {} , error = {}  ",
                elevator_id,
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.loadElevatorInfo(elevator_id) execute success , elevator_id = {} ", 
            elevator_id
            );
        
        if (elevator_loucen_info == null)
        {
            logger.debug("==> elevator loucen info is null ");
            throw new WrenchElevatorInstallException(Result.ELEVATOR_LOUCEN_INFO_IS_NULL_CODE, Result.ELEVATOR_LOUCEN_INFO_IS_NULL_MESSAGE);
        }
        return elevator_loucen_info;
     }
    
    
    /**
     * 获取电梯列表
     */
    public void loadElevatorList(String user_id,String power,ModelMap params)
        throws WrenchElevatorInstallException
        {
           if (WrenchElevatorInstallUtil.hasNull(user_id,power,params))
           {
               logger.error("==> param is not found , user_id = {} , power = {} , params = {}",
                   user_id,
                   power,
                   params
                   );
               throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
           }
           if (!WrenchElevatorInstallUtil.areInt(user_id))
           {
               logger.error("==> string is not int type , user_id = {}  ", user_id);
               throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
           }
           
           List<Map<String,Object>> elevatorList = null;
           try
           {
               elevatorList = dao.loadElevatorList(user_id, power);
           }
           catch (Exception e)
           {
               logger.error("==> dao.loadElevatorList(user_id, power) execute error , user_id = {} , power = {} , error = {}  ",
                   user_id,
                   power,
                   e);
               throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
           }
           
           
           
           if (elevatorList.size() > 0)
           {
           for (int i = 0; i<elevatorList.size();i++)
           {
               //字符串转JSONArray
               String jsonMessage = (String)(elevatorList.get(i).get("loucen_info"));
               if (jsonMessage == null)
               {
                   elevatorList.get(i).remove("loucen_info");
                   elevatorList.get(i).put("flag", false);
                   continue;
               }
               else
               {
                   logger.debug("========>"+jsonMessage);
                   TreeMap t=new TreeMap((JSON.parseObject(jsonMessage)));
                   String start_loucen = null;
                   String end_loucen = null;
                   
                   start_loucen = ((String)((JSONObject)t.firstEntry().getValue()).get("name"));
                   end_loucen = ((String)((JSONObject)t.lastEntry().getValue()).get("name"));
                   
                   elevatorList.get(i).put("start_loucen", start_loucen);
                   elevatorList.get(i).put("end_loucen", end_loucen);
                   //将原来的楼层详细信息删除
                   elevatorList.get(i).remove("loucen_info");
                   elevatorList.get(i).put("flag", true);
               }
              
              
           }
           }
           
           
           params.put("elevatorList", elevatorList);
           
           logger.debug("==> dao.loadElevatorList(user_id, power) execute success , user_id = {} , power = {} ", 
               user_id,
               power
               );
        }
        
           
}
